﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.AccessControl;
using System.IO;

namespace DLLTest.OpertionClass
{
    class FileAccessControl
    {
       #region 方法
        //http://ditiedetiaozao.blog.163.com/blog/static/18041287201402185848240/
        //为指定帐户的指定文件增加权限 
        public static void AddFileSecurity(string MyFileName, string MyAccount,
            FileSystemRights MyRights, AccessControlType MyControlType)
        {
            FileSecurity MySecurity = File.GetAccessControl(MyFileName);
            MySecurity.AddAccessRule(new FileSystemAccessRule(MyAccount,
                MyRights, MyControlType));
            File.SetAccessControl(MyFileName, MySecurity);
        }
        //为指定帐户的指定文件移除权限 
        public static void RemoveFileSecurity(string MyFileName, string MyAccount,
            FileSystemRights MyRights, AccessControlType MyControlType)
        {
            FileSecurity MySecurity = File.GetAccessControl(MyFileName);
            MySecurity.RemoveAccessRule(new FileSystemAccessRule(MyAccount,
                MyRights, MyControlType));
            File.SetAccessControl(MyFileName, MySecurity);
        }
        #endregion
    }
}
